#define _CRT_SECURE_NO_WARNINGS 1
//#include <fstream>
//#include <iostream>
//#include <string>
//#include <sstream>
//#include<vector>
//#include<algorithm>
//using namespace std;

#include"BinaryTree.h"

BTNode* BinaryTreeCreate(BTDataType* a, int* pi)
{

		if (a[(*pi)] == '#')
		{
			(*pi)++;
			return NULL;
		}

		BTNode* root = (BTNode*)malloc(sizeof(BTNode));
		if (root == NULL)
		{
			perror("malloc fail");
		}
		root->data = a[(*pi)++];
		root->left = BinaryTreeCreate(a, pi);
		root->right = BinaryTreeCreate(a,  pi);

		return root;
	
}

int BinaryTreeSize(BTNode* root)
{
	if (root == NULL)
		return 0;
	return BinaryTreeSize(root->left) + BinaryTreeSize(root->right) + 1;
}

void BinaryTreePrevOrder(BTNode* root)
{
	if (root == NULL)
		return;
	if (root->data != '#')
	{
		printf("%c ", root->data);
	}
	BinaryTreePrevOrder(root->left);
	BinaryTreePrevOrder(root->right);

}

void BinaryTreeInOrder(BTNode* root)
{
	if (root == NULL)
		return;
	
	BinaryTreePrevOrder(root->left);
	if (root->data != '#')
	{
		printf("%c ", root->data);
	}
	BinaryTreePrevOrder(root->right);
}

void BinaryTreePostOrder(BTNode* root)
{
	if (root == NULL)
		return;

	BinaryTreePrevOrder(root->left);
	BinaryTreePrevOrder(root->right);
	if (root->data != '#')
	{
		printf("%c ", root->data);
	}
	
}

int BinaryTreeLeafSize(BTNode* root)
{
	if (root == NULL)
		return 0;
	if (root->left == NULL && root->right == NULL)
		return 1;
	return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);

}

int BinaryTreeLevelKSize(BTNode* root, int k)
{
	if (root == NULL)
		return 0;
	if (k == 1)
		return 1;
    
	return BinaryTreeLevelKSize(root->left, k-1)+BinaryTreeLevelKSize(root->right, k-1);

}

BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
	if (root == NULL)
		return NULL;
	if (root->data == x)
		return root;
    BTNode* ret1 =  BinaryTreeFind(root->left, x);
	BTNode* ret2 = BinaryTreeFind(root->right, x);
	
	if (ret1 != NULL)
		return ret1;
	if (ret2 != NULL)
		return ret2;
	return NULL;
}

void BinaryTreeLevelOrder(BTNode* root)
{
	Queue q;
	QueueInit(&q);
	if (root)
		QueuePush(&q, root);
	while (!QueueEmpty(&q))
	{
		BTNode* front = QueueFront(&q);
		printf("%c ", front->data);
		QueuePop(&q);
		if (front->left)
			QueuePush(&q, front->left);
		if (front->right)
			QueuePush(&q, front->right);
	}
	printf("\n");
	QueueDestroy(&q);

}

int BinaryTreeHeight(BTNode* root)
{
	if (root == NULL)
		return 0;
	else
		return 1;
	return fmax(BinaryTreeHeight(root->left) + 1, BinaryTreeHeight(root->right) + 1);
}
bool BinaryTreeComplete(BTNode* root)
{
	Queue q;
	QueueInit(&q);
	if (root)
		QueuePush(&q, root);
	while (!QueueEmpty(&q))
	{
		BTNode* front = QueueFront(&q);
		if (front == NULL)
			break;
		QueuePop(&q);
		QueuePush(&q, front->left);
		QueuePush(&q, front->right);
	}
	while (!QueueEmpty(&q))
	{
		BTNode* front = QueueFront(&q);
		if (front)
			return false;
		QueuePop(&q);
		
	}

	return true;
	QueueDestroy(&q);
}
void BinaryTreeDestory(BTNode** root)
{

}